﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Newtonsoft.Json;
using Org.BouncyCastle.Crypto.Tls;
using WebGrease.Css.Extensions;
using Zero.Code;
using Zero.BLL;
using Zero.Model;

namespace Zero.Framework.Areas.SystemManage.Controllers
{
    /// <summary>
    /// 
    /// </summary>
    public class OrganizationsController : Controller
    {
        // GET: SystemManage/Organizations
        public ActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 获取机构下拉项
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [HandlerAjaxOnly]
        public ActionResult GetTreeSelectJson()
        {
            int roleId = OperatorProvider.Provider.GetCurrentNoEncrypt().RoleId.ToInt();//获取当前用户角色ID
            var data = new BLL.Organization().GetOrganizationsByRoleID(roleId);
            var treeList = new List<TreeSelectModel>();
            foreach (organization item in data)
            {
                TreeSelectModel treeModel = new TreeSelectModel();
                treeModel.id = item.O_Id;
                treeModel.text = item.O_Name;
                treeModel.parentId = item.O_ParentId;
                treeModel.data = item;
                treeList.Add(treeModel);
            }

            var json = treeList.TreeSelectJson();
            return Content(treeList.TreeSelectJson());
        }


        [HttpPost]
        [HandlerAjaxOnly]
        /// <summary>
        /// 通过机构ID获取机构信息集合
        /// </summary>
        /// <param name="OrganizedID">机构ID</param>
        /// <returns></returns>
        public string GetOrganizationByID(int OrganizedID)
        {
            return Code.Json.ToJson(new BLL.Organization().GetOrganizationByID(OrganizedID));
        }

        public string GetOrganizationName(string RoleId)
        {
            var O_ID = new BLL.RoleAuthorization().GeyDutyById(RoleId.ToInt()).R_OrganizedId;
            return GetOrganizationByID(O_ID.ToInt());
        }
    }
}